﻿<%@ WebHandler Language="C#" Class="GetProductHandler" %>

using System;
using System.Web;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

public class GetProductHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        var data = new List<Product>() {            
                new Product { ProductID="9", ProductCode="0901", SearchString="0901 - Cá hồi"},
                new Product { ProductID="10", ProductCode="090101", SearchString="090101 - Cá hồi luộc"},
                new Product { ProductID="11", ProductCode="09010101", SearchString="09010101 - Cá hồi luộc rau muống"},
                new Product { ProductID="12", ProductCode="09010102", SearchString="09010102 - Cá hồi luộc rau đay"},
                new Product { ProductID="13", ProductCode="090102", SearchString="090102 - Cá hồi nướng"},
                new Product { ProductID="14", ProductCode="09010201", SearchString="09010201 - Cá hồi nướng trui"},
                new Product { ProductID="15", ProductCode="09010202", SearchString="09010202 - Cá hồi nướng mỡ hành"},
                new Product { ProductID="16", ProductCode="0902", SearchString="0902 - Cá lốc"},
                new Product { ProductID="17", ProductCode="090201", SearchString="090201 - Cá lốc luộc"},
                new Product { ProductID="18", ProductCode="09020101", SearchString="09020101 - Cá lốc luộc cà"},
                new Product { ProductID="19", ProductCode="09020102", SearchString="09020102 - Cá lốc luộc mướp"},
                new Product { ProductID="20", ProductCode="09020103", SearchString="09020103 - Cá lốc luộc rau-gì-quên-tên-òi"},
                new Product { ProductID="21", ProductCode="090202", SearchString="090202 - Cá lốc nướng"},
                new Product { ProductID="22", ProductCode="09020201", SearchString="09020201 - Cá lốc nướng trui"},
                new Product { ProductID="23", ProductCode="09020202", SearchString="09020202 - Cá lốc nướng mỡ hành"}
            };

        int limit = 0;
        if (HttpContext.Current.Request.QueryString["limit"] != null)
            limit = Convert.ToInt32(HttpContext.Current.Request.QueryString["limit"]);

        string q = "";
        if (HttpContext.Current.Request.QueryString["q"] != null)
            q = HttpContext.Current.Request.QueryString["q"];

        var sb = new StringBuilder();
        if (q.Trim() != "")
        {
            var query = data.Where(p => p.SearchString.ToLower().Contains(q)).OrderBy(p => p.ProductID);
            List<Product> result = null;
            if (limit > 0)
                result = query.Take(limit).ToList();
            else
                result = query.ToList();

            foreach (var item in result)
                sb.AppendFormat("{0}{1}|{2}|{3}", (sb.Length > 0 ? "\n" : ""), item.SearchString,item.ProductCode, item.ProductID );
        }

        // Define Response type = text
        context.Response.ContentType = "text/plain";
        context.Response.Write(sb.ToString());
    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

